package com.qtcm.nas.wzh.commoditys.controller;

import com.qtcm.nas.wzh.commoditys.pojo.Commodity;
import com.qtcm.nas.wzh.commoditys.pojo.Commoditya;
import com.qtcm.nas.wzh.commoditys.service.ICommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;


@Controller
@RequestMapping("/CommodityController")
public class CommodityController {
        @Autowired
        @Qualifier("commodityService")
    private ICommodityService commodityService;
        @RequestMapping("/findAll")
        public String findAll(Model model) {
        System.out.println("调用到了控制层-Controller-使用了查询所以方法");
        List<Commodity> all = commodityService.findAll();
        model.addAttribute("list", all);
        return "goods_List";
    }
    @RequestMapping("/addCommodity")
    public String addCommodity(Commodity commodity){
            System.out.println(commodity.toString());
        commodityService.addCommodity(commodity);
        return "redirect:findAll";
    }
    @RequestMapping("/updateCommodity")
    public String updateCommodity(Commodity commodity){
        commodityService.updateCommodity(commodity);
        return "redirect:findAll";
    }
    @RequestMapping("/findByid")
    public String findById(Model model, int J_letem_id) {
        Commodity commodity = commodityService.findById(J_letem_id);
        model.addAttribute("list", commodity);
        System.out.println(J_letem_id);
        return "edit_Goods";
    }
    @RequestMapping("/deleteCommodity")
    public String deleteCommodity(int J_letem_id) {
        commodityService.deleteCommodity(J_letem_id);
        return "forward:findAll";
    }
    @RequestMapping("/updatestate")
    public String updatestate(int J_letem_id,String J_state){
       Commodity commodity = new Commodity();
        if (J_state.equalsIgnoreCase("0")){
            commodity.setJ_state("1");
            commodity.setJ_letem_id(J_letem_id);
            commodityService.updatestate(commodity);

        }else if (J_state.equalsIgnoreCase("1")) {
            commodity.setJ_state("0");
            commodity.setJ_letem_id(J_letem_id);
            commodityService.updatestate(commodity);

        }
        return"redirect:findAll";
    }
    //另外一个


    @RequestMapping("/findAlla")
    public String findAlla(Model model) {
        System.out.println("调用到了控制层-Controller-使用了查询所以方法");
        List<Commoditya> all = commodityService.findAlla();
        model.addAttribute("list", all);
        return "goods_CLassify";
    }
    @RequestMapping("/addCommoditya")
    public String addCommoditya(Commoditya commoditya){
        commodityService.addCommoditya(commoditya);
        return "redirect:findAlla";
    }


    @RequestMapping("/updateCommoditya")
    public String updateCommoditya(Commoditya commoditya){
        commodityService.updateCommoditya(commoditya);
        return "redirect:findAlla";
    }


    @RequestMapping("/findByida")
    public String findByIda(Model model, int J_commodity_classification) {
        Commoditya commoditya=commodityService.findByIda(J_commodity_classification);
        model.addAttribute("list", commoditya);
        return "update_Goodsa";
    }
    @RequestMapping("/deleteCommoditya")
    public String deleteCommoditya(int J_commodity_classification) {
        commodityService.deleteCommoditya(J_commodity_classification);
        return "forward:findAlla";
    }
}
